/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rdfparser.parser;

import rdfparser.parser.ParserHandler;
import org.xml.sax.Attributes;
import rdfparser.bean.ForumBean;

/**
 *
 * @author Amendra Shrestha
 */
public class ForumParser extends ParserHandler {

    private String parsedValue;
    private boolean isMessageBoard = false;
    private boolean hasParent = false;
    private boolean ofParent = false;
    private ForumBean forum;
    private ForumBean.ForumParent parentThread;

    /**
     * parses the XML document file prints the parse data of XML document file
     * adds parse data of XML document file to database
     *
     * @param baseDir
     * @param fileDir
     * @param fileName
     */
    public void parseForum(String fileName) {

        if (parseDocument(fileName)) {
            System.out.println("\tParsed document successfully.");
            printDocument();
            addData(fileName);
        } else {
            System.out.println("\tException occured while parsing document.");
        }
    }

    /**
     * overriding the default SAX start element method parses each starting XML
     * element tag and sets the required extracted data
     *
     * @param uri
     * @param localName
     * @param elementName
     * @param attributes
     */
    @Override
    public void startElement(String uri,
            String localName,
            String elementName,
            Attributes attributes) {
        if (elementName.equalsIgnoreCase("sioct:MessageBoard")) {
            forum = new ForumBean();
            forum.setMessageBoard(attributes.getValue("rdf:about"));
            isMessageBoard = true;
        } else if (elementName.equalsIgnoreCase("rdf:type") && isMessageBoard) {
            forum.setType(attributes.getValue("rdf:resource"));
        } else if (elementName.equalsIgnoreCase("sioc:link") && isMessageBoard) {
            forum.setLink(attributes.getValue("rdf:resource"));
        } else if (elementName.equalsIgnoreCase("sioc:has_parent")) {
            hasParent = true;
        } else if (elementName.equalsIgnoreCase("sioc:Forum") && hasParent) {
            forum.setparentForum(attributes.getValue("rdf:about"));
        } else if (elementName.equalsIgnoreCase("rdfs:seeAlso") && hasParent) {
            forum.setParentForumseeAlso(attributes.getValue("rdf:resource"));
            hasParent = false;
        } else if (elementName.equalsIgnoreCase("sioc:parent_of")) {
            ofParent = true;
            parentThread = forum.new ForumParent();
        } else if (elementName.equalsIgnoreCase("sioc:Thread") && ofParent) {
            parentThread.setThread(attributes.getValue("rdf:about"));
        } else if (elementName.equalsIgnoreCase("rdfs:seeAlso") && ofParent) {
            parentThread.setSeeAlso(attributes.getValue("rdf:resource"));
            ofParent = false;
        } else if (elementName.equalsIgnoreCase("rdfs:seeAlso") && isMessageBoard) {
            forum.setSeeAlso(attributes.getValue("rdf:resource"));
            isMessageBoard = false;
        }
    }

    /**
     * overriding the default SAX end element method parses each ending XML
     * element tag and sets the required extracted data
     *
     * @param namespaceURI
     * @param localName
     * @param elementName
     */
    @Override
    public void endElement(String namespaceURI,
            String localName,
            String elementName) {
        if (elementName.equalsIgnoreCase("dc:title") && isMessageBoard) {
            isMessageBoard = true;
            forum.setTitle(parsedValue);
        } else if (elementName.equalsIgnoreCase("dc:description") && isMessageBoard) {
            forum.setDescription(parsedValue);
        } else if (elementName.equalsIgnoreCase("sioc:parent_of")) {
            forum.getPostParentList().add(parentThread);
        }
    }

    /**
     * holds extracted text data in string object
     *
     * @param ch
     * @param start
     * @param length
     * @throws SAXException
     */
    @Override
    public void characters(char ch[],
            int start,
            int length) {
        parsedValue = new String(ch, start, length);
    }

    /**
     * prints parsed data of XML document
     */
    private void printDocument() {
        System.out.println("Message Board: " + forum.getMessageBoard());
        System.out.println("Type: " + forum.getType());
        System.out.println("Link: " + forum.getLink());
        System.out.println("Title: " + forum.getTitle());
        System.out.println("Descripton: " + forum.getDescription());
        System.out.println("Parent Forum: " + forum.getparentForum());
        System.out.println("Parent Forum seeAlso: " + forum.getParentForumseeAlso());
        System.out.println("seeAlso: " + forum.getSeeAlso());

        System.out.println("\nParent Thread:");
        System.out.println("-----------------");
        for (ForumBean.ForumParent list : forum.getPostParentList()) {
            System.out.println("Parent Thread: " + list.getThread());
            System.out.println("Parent seeAlso: " + list.getSeeAlso() + '\n');
        }
    }

    /**
     * adds parsed data of XML document in db
     */
    private void addData(String filename) {
        System.out.println("Adding data.......");
        forum.addForum(filename);
        forum.addforumParent();
        System.out.println("Successfully added data....");
    }
}